import moment from "moment";
import ProtocolSignUtils from "../common/Utils/ProtocolSignUtils";
import XLog from "../libs/XLog";
import DataCenter from "./DataCenter";
import {ACK_Type, Adjust_Model, MQTT_CONNECT_STATUS, onlineStatus, OperationResult, ProductID, Topic_Control, ValidType} from "./LogicConstant"

export function commonMakeParams(params, signKey) {
    let req = {
        "gateId": DataCenter.gateId,
        "serverId": DataCenter.serverId,
        "timeStamp": ""+Date.now(),
        ...params
    };
    if(!signKey) {
        return req;
    }
    else {
        let sign = ProtocolSignUtils.getSignString(req, signKey);

        return {
            ...req,
            sign
        }
    }
}

export function transControl2Adjust(data) {
    let loops = [];
    let controlType = data.controlType;
    let controls = data.control;
    for(var control of controls){
        let loop = {};
        loop.loopNumber = control.loopNumber;
        loop.loopId = control.loopId || 0;
        loop.brightness = control.brightness || "";
        loop.strategyId = control.strategyId || 0;
        loops.push(loop);
    }
    let params_control = {
        tunnelId: DataCenter.tunnelId,
        tunnelType: DataCenter.tunnelType,
        sectionId: data.sectionId || 0,
        groupId: 0,
        gateId: data.controlGateId || 0,
        schemaId: 0,
        strategyId: data.strategyId || 0,
        brightness: data.brightness || "",
        loops: loops
    }
    let req = {
        "gateId": DataCenter.gateId,
        "serverId": DataCenter.serverId,
        "timeStamp": ""+Date.now(),
        "controlType": controlType,
        "control": params_control
    };
    let sign = ProtocolSignUtils.getSignString(req, DataCenter.gateSecret);
    return {
        ...req,
        sign
    }
}

export function commonMakeMqttRequest(ack, params, method, sign=DataCenter.gateSecret) {
    return {
        "id": ""+Date.now(),
        "sys":{
            "ack": ack
        },
        "params":commonMakeParams(params, sign),
        "method": method
    }
}

export function commonMakeRsp2Request(data, method) {
    return {
        "id": ""+Date.now(),
        "gateId": DataCenter.gateId,
        "code": 200,
        "message": "",
        "data": data,
        "method": method
    }
}

export function commonParseMqttRsp(data) {
    try{
        return JSON.parse(data);
    }catch(error) {
        XLog.e("commonParseMqttRsp", data);
    }
    return null;
}

export function commonMakeControlData(control){
    let result = [];
    for (var loop of control){
        result.push({
            "gateId": loop.gateId,
            "loopNumber": loop.loopNumber,
            "status": OperationResult.SUCCESS,
            "flowId": loop.flowId || 0
        });
    }
    return result;
}

export function commonMakeGateBrightControlCmd(loopNumber, brightness) {
    return {
        loopNumber: loopNumber,
        strategyId: 0,
        brightness: ""+brightness,
    }
}

export function commonMakeGateStrategyControlCmd(loopNumber, strategyId) {
    return {
        loopNumber: loopNumber,
        strategyId: strategyId,
        brightness: "",
    }
}

export function commonMakeControlRequest(level, needCache, seciontId, gateId, model, value, loops, needAdjust= true ) {
    return commonMakeMqttRequest(ACK_Type.NO_ACK, 
        {needAdjust: needAdjust,
        needCache: needCache, 
        controlType: level,
        sectionId: seciontId || 0,
        gateId: DataCenter.gateId,
        controlGateId: gateId || 0,
        sendGateId: DataCenter.gateId,
        brightness: model == Adjust_Model.ADJUST_DIRECT_LIGHT ? (""+value) : "",
        strategyId: model == Adjust_Model.ADJUST_STRATEGY_LIGHT ? value : 0,
        control: loops||[]}, 
        Topic_Control.method, "")
}

export function queryStrategyBrightness(setting) {
    if(setting) {
        let conf = JSON.parse(setting);
        let now = moment().format("HH:mm");
        let j = 0;
        for (j=0; j<conf.length; j++) {
            if (now < conf[j].time) {
                break;
            }
        }
        if (j === 0
            || j === conf.length) {
            return ""+conf[conf.length-1].light;
        } else {
            return "" + conf[j-1].light;
        }

    }
    return "";
}

export function checkIpValid(ip) {
    return ip && ip != "0.0.0.0";
}

export function commonMakeMeterRequest(id, data, type, method) {
    return commonMakeMqttRequest(ACK_Type.NO_ACK, 
        {
            "productId": ProductID.SMART,
            "identifierId": type,
            "onlineStatus": onlineStatus.ONLINE,
            "valid": ValidType.VALID,
            "meterList": data,
            "id": id,
        }, 
        method);
}

export function formatterMqttConnectState(state) {
    if(state == MQTT_CONNECT_STATUS.CONNECT_SUCCESS) {
        return "连接成功";
    } else if(state == MQTT_CONNECT_STATUS.CONNECT_CLOSE) {
        return "连接关闭";
    } else if(state == MQTT_CONNECT_STATUS.CONNECT_ERROR) {
        return "连接错误";
    } else if(state == MQTT_CONNECT_STATUS.CONNECT_NONE) {
        return "无连接";
    } else {
        return "未知状态：" + state;
    }
}